using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;

public class Log
{
    private static bool folderExist = false;
    private static bool chatFolderExist = false;
    private static object fileLock = new object();
    private static object chatFileLock = new object();
    public static void outputLog(string severity, string msg)
    {
        if (!folderExist)
        {
            if (!System.IO.Directory.Exists("Log"))
            {
                System.IO.Directory.CreateDirectory("Log");
            }
            folderExist = true;
        }
        lock (fileLock)
        {
            File.AppendAllText("Log/RegionServer_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log", "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + severity.ToUpper() + " : " + msg.Trim() + "\r\n");
        }
    }
    public static void ChatLog(string from, string to, string type, string content)
    {
        if (!chatFolderExist)
        {
            if (!System.IO.Directory.Exists("ChatLog"))
            {
                System.IO.Directory.CreateDirectory("ChatLog");
            }
            chatFolderExist = true;
        }
        lock (chatFileLock)
        {
            //string from, string to, string type, string content, string tick
            File.AppendAllText("ChatLog/Chat_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log",
                "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + from + ":" + from + ",to:" + to + ",type:" + type + ",content:" + content + "\r\n");
        }
    }

    public static int ShowLog = int.Parse(ConfigurationSettings.AppSettings["loglevel"]);
    public static void Debug(string debug)
    {
        if (ShowLog <= 1)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(debug);
            outputLog("Debug", debug);
        }
    }
    public static void Info(string debug)
    {
        if (ShowLog <= 2)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(debug);
            outputLog("Info", debug);
        }
    }
    public static void Warning(string debug)
    {
        if (ShowLog <= 3)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(debug);
            outputLog("Warning", debug);
        }
    }
    public static void Error(string debug)
    {
        if (ShowLog <= 4)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(debug);
            outputLog("Error", debug);
        }
    }
}